import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.StringTokenizer;


public class Encode 
{
	
	public static void main(String[] args)
	{
		String inputFile1  = args[0];
		String inputFile2  = args[1];
		String outputFile1 = args[2];
		String outputFile2 = args[3];
		String outputFile3 = args[4];
		
		/*String folder=".\\src\\Example5\\";
		String inputFile1  = folder+"input1.txt";
		String inputFile2  = folder+"input2.txt";
		String outputFile1 = folder+"out3.txt";
		String outputFile2 = folder+"out4.txt";
		String outputFile3 = folder+"out5.txt";*/
		
		String[] newArgs=new String[3];
		//Learn				input1-->input1_learned.txt
		newArgs[0]=inputFile1;
		newArgs[1]=inputFile1+"_learned.txt";
		Learn.main(newArgs);
		//BuildHuffman		input1_learned.txt-->input1_built.txt
		newArgs[0]=inputFile1+"_learned.txt";
		newArgs[1]=inputFile2+"_built.txt";
		BuildHuffman.main(newArgs);
		//HuffmanEncode		input1_learned.txt + inputFile2 -> output1.txt
		newArgs[0]=inputFile1;
		newArgs[1]=inputFile2;
		newArgs[2]=outputFile1;
		HuffmanEncode.main(newArgs);
		//LZEncode			input1->outputFile2
		newArgs[0]=inputFile1;
		newArgs[1]=outputFile2;
		LZEncode.main(newArgs);
		
		//getstatistics
		String[] stats=new String[3];
		stats[0]=""+get_File_String(inputFile1).length()*5;
		stats[1]=""+get_File_String(outputFile1).length();
		stats[2]=""+get_File_String(outputFile2).replaceAll("\\(|\\)|\\,", "").length()*5;
		System.out.println(stats[2]);
		
		write_To_File(outputFile3, stats);
		
		//TODO
		/*
		 * inputFile1 - The Text to be encoded
		 * inputFile2 - The Huffman code
		 * outputFile1 - Encoding of the Text using Huffman code
		 * outputFile2 - Encoding of the Text using LZ algorithm
		 * outputFile3 - Statistical details
		 * 
		 *  
		 *  
		 */
	}
	private static void write_To_File(String outputFilename,String[] encodedText) {

        System.out.println("Writing to file " + outputFilename);
        
        
        try {
            File outFile = new File(outputFilename);
            FileWriter ofw = new FileWriter(outFile);
            System.out.println("writing to file ....\n"+outputFilename);
            for (int i=0;i<encodedText.length;i++){
            	if (encodedText!=null && encodedText.length>0){
            		System.out.println(encodedText[i]);
                    ofw.write(encodedText[i]+"\r\n");
            	}
            }
            
            ofw.close();
            
            System.out.println("\nWrote to file " + outputFilename);
            
        }
        catch (Exception e) {
            System.out.println( "Error \""+ e.toString()+ "\" on file "+outputFilename);
            e.printStackTrace() ;
            System.exit(-1) ;      //brutally exit the program
        }

    }
	public static String get_File_String(String inputFilename){
       	System.out.println("Reading from file " + inputFilename);
       	String ans="";
       	try {
            File inFile = new File(inputFilename);
            FileReader ifr = new FileReader(inFile);
            String fullfile=ifr.toString();
            System.out.println("File length is "+fullfile.length()+"File string is \n"+fullfile);
            BufferedReader ibr= new BufferedReader(ifr) ;

            String line = "" ;
            while (line != null )
            {
                line = ibr.readLine() ;
                if (line != null)
                {
                    StringTokenizer st = new StringTokenizer(line); // splits the line into tokens
					
                    while(st.hasMoreTokens())
                    {
                    	String token = st.nextToken().trim();
                    	System.out.println(token);
                    	ans=ans+token;
                    }                   
                }
            }

            ibr.close();
            ifr.close();
        }

        catch(Exception e) {
            System.out.println( "Error \""+ e.toString()+ "\" on file "+inputFilename);
            e.printStackTrace() ;
            System.exit(-1) ;      //brutally exit the program
        }
        return ans;
    }
}
